home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / Word Services SDK 1.0.6 / Writeswell Jr 1.2.1 Sources ƒ / Writeswell Jr. Source / FontMenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-11  |  8.8 KB  |  410 lines  |  [TEXT/KAHL]

  1. /* FontMenu.c
  2.  * Handle font and style menus in Writeswell Jr.
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  *
  14.  * 19 May 92 Mike Crawford
  15.  */
  16.  
  17. #include <EPPC.h>
  18. #include <AppleEvents.h>
  19. #include <AEObjects.h>
  20. #include <Script.h>
  21. #include "AERegistry.h"
  22. #include "WordServices.h"
  23. #include "TestBed.h"
  24. #include "TBConstants.h"
  25. #include "Gripe.h"
  26. #include "AppEvents.h"
  27. #include "TBGlobals.h"
  28. #include "FontMenu.h"
  29. #include "OutlineButton.h"
  30.  
  31. void FixPasteItem( MenuHandle editMenu );
  32.  
  33. static Style gStyleTable[] = { 0, bold, italic, underline, outline, shadow, condense, extend };
  34. static short gPointSizeTable[] = { 9, 10, 12, 14, 18, 24, 48, 72 };
  35.  
  36. Boolean DoFontMenu( short theItem )
  37. {
  38.     TEHandle    textH;
  39.     TextStyle    styleRec;
  40.     MenuHandle    fontMenu;
  41.     short        fontNum;
  42.     Str255        fontName;
  43.  
  44.     if ( !gDocWindow )
  45.         return true;
  46.         
  47.     if ( gReadOnly )
  48.         return true;
  49.  
  50.     textH = (TEHandle)GetWRefCon( gDocWindow );
  51.     
  52.     fontMenu = GetMHandle( kFontMenuID );
  53.     if ( !fontMenu )
  54.         return false;
  55.     
  56.     GetItem( fontMenu, theItem, fontName );
  57.     
  58.     GetFNum( fontName, &fontNum );
  59.     
  60.     /* Set TE to use the new font */
  61.  
  62.     styleRec.tsFont = fontNum;
  63.  
  64.     TESetStyle( doFont, &styleRec, true, textH );
  65.  
  66.     gDocDirty = true;
  67.     
  68.     return true;
  69. }
  70.  
  71. Boolean DoStyleMenu( short theItem )
  72. {
  73.     TEHandle textH;
  74.  
  75.     if ( theItem == kStMOtherPoint ){
  76.         CustomSize();
  77.         return true;
  78.     }
  79.  
  80.     /* 2.0 MDC handle read-only documents */
  81.     
  82.     if ( gReadOnly )
  83.         return true;
  84.  
  85.     gDocDirty = true;
  86.  
  87.     if ( theItem < kStMDash ){
  88.         SetStyle( theItem );
  89.         return true;
  90.     }
  91.     
  92.     SetSize( theItem );
  93.     
  94.     return true;
  95. }
  96.  
  97. void CustomSize( void )
  98. {
  99.     TextStyle    styleRec;
  100.     short        fontAscent;
  101.     short        lineHeight;
  102.     TEHandle    textH;
  103.     DialogPtr    custDlg;
  104.     short        item;
  105.     Str255        textStr;
  106.     short        kind;
  107.     Handle        h;
  108.     Rect        r;
  109.     long        num;
  110.     UserItemUPP    outlineUPP;
  111.     
  112.     if ( !gDocWindow )
  113.         return;
  114.         
  115.     textH = (TEHandle)GetWRefCon( gDocWindow );
  116.  
  117.     custDlg = GetNewDialog( kCustomSizeID, (Ptr)NULL, (WindowPtr)-1 );
  118.     if ( !custDlg )
  119.         return;
  120.     
  121.     /* Get the current size */
  122.  
  123.     TEGetStyle( (*textH)->selStart, &styleRec, &lineHeight, &fontAscent, textH );
  124.     NumToString( (long)fontAscent, textStr );
  125.     
  126.     GetDItem( custDlg, kCSEntryField, &kind, &h, &r );
  127.     SetIText( h, textStr );
  128.     
  129.     TESetSelect( 0L, 32000L, ((DialogPeek)custDlg)->textH );
  130.  
  131.     /* Set up a user proc to draw the default outline */
  132.  
  133.     GetDItem( custDlg, kCSDefUser, &kind, &h, &r );
  134.     
  135.     outlineUPP = NewUserItemProc( OutlineButton );
  136.     if ( !outlineUPP )
  137.         return;                // STUB should report an error 
  138.  
  139.     SetDItem( custDlg, kCSDefUser, kind, (Handle)outlineUPP, &r );
  140.     
  141.     do {
  142.         ModalDialog( (ModalFilterUPP)NULL, &item );
  143.     } while ( item != kCSCancel && item != kCSOK );
  144.     
  145.     if ( item == kCSCancel ){
  146.         DisposDialog( custDlg );
  147.         return;
  148.     }
  149.     
  150.     GetDItem( custDlg, kCSEntryField, &kind, &h, &r );
  151.     GetIText( h, textStr );
  152.  
  153. #ifdef GENERATINGCFM
  154.     DisposeRoutineDescriptor( outlineUPP );
  155. #endif
  156.     
  157.     DisposDialog( custDlg );
  158.  
  159.     StringToNum( textStr, &num );
  160.     
  161.     /* Set TE to use the new style */
  162.  
  163.     styleRec.tsSize = (short)num;
  164.  
  165.     TESetStyle( doSize, &styleRec, true, textH );
  166.  
  167.     gDocDirty = true;            /* Note that it's not dirty if we canceled */
  168.     
  169.     return;
  170. }
  171.  
  172. void SetStyle( short theItem )
  173. {
  174.     TextStyle    styleRec;
  175.     TEHandle    textH;
  176.  
  177.     if ( !gDocWindow )
  178.         return;
  179.         
  180.     textH = (TEHandle)GetWRefCon( gDocWindow );
  181.     
  182.     /* Set TE to use the new style */
  183.  
  184.     styleRec.tsFace = gStyleTable[ theItem - 1 ];
  185.  
  186.     TESetStyle( doFace, &styleRec, true, textH );
  187.  
  188.     return;
  189. }
  190.  
  191. void SetSize( short theItem )
  192. {
  193.     TextStyle    styleRec;
  194.     TEHandle    textH;
  195.  
  196.     if ( !gDocWindow )
  197.         return;
  198.         
  199.     textH = (TEHandle)GetWRefCon( gDocWindow );
  200.  
  201.     theItem -= kStM9Point;
  202.     
  203.     styleRec.tsSize = gPointSizeTable[ theItem ];
  204.     
  205.     TESetStyle( doSize, &styleRec, true, textH );
  206.     
  207.     return;
  208. }
  209.  
  210. void InitStyleMenu( void )
  211. {
  212.     MenuHandle    styleMenu;
  213.     short        i;
  214.  
  215.     styleMenu = (MenuHandle)GetResource( 'MENU', kStyleMenuID );
  216.     if ( !styleMenu ){
  217.         Gripe( "\pcannot get Style Menu Handle" );
  218.         return;
  219.     }
  220.     
  221.     for ( i = kStMBold; i < kStMDash; i++ ){
  222.         SetItemStyle( styleMenu, i, gStyleTable[ i - 1 ] );
  223.     }
  224.  
  225.     return;
  226. }
  227.  
  228. void FixMenuMarks( void )
  229. {
  230.     TextStyle    styleRec;
  231.     TEHandle    textH;
  232.     MenuHandle    fileMenu;
  233.     MenuHandle    fontMenu;
  234.     MenuHandle    styleMenu;
  235.     MenuHandle    editMenu;
  236.     short        numItems;
  237.     short        i;
  238.     short        fontNum;
  239.     short        itemSize;
  240.     short        lineHeight;
  241.     short        fontAscent;
  242.     Str255        fontName;
  243.     Str255        itemName;
  244.     short        mode;
  245.     Boolean        continuous;
  246.  
  247.     fileMenu = (MenuHandle)GetResource( 'MENU', kFileMenuID );
  248.     if ( !fileMenu )
  249.         return;
  250.  
  251.     editMenu = (MenuHandle)GetResource( 'MENU', kEditMenuID );
  252.     if ( !editMenu )
  253.         return;
  254.  
  255.     fontMenu = (MenuHandle)GetResource( 'MENU', kFontMenuID );
  256.     if ( !fontMenu )
  257.         return;
  258.  
  259.     styleMenu = (MenuHandle)GetResource( 'MENU', kStyleMenuID );
  260.     if ( !styleMenu )
  261.         return;
  262.  
  263.     DisableItem( editMenu, kEMUndo );
  264.  
  265.     if ( !gDocWindow ){
  266.         
  267.         EnableItem( fileMenu, kFMNew );
  268.         EnableItem( fileMenu, kFMOpen );
  269.         DisableItem( fileMenu, kFMClose );
  270.         DisableItem( fileMenu, kFMSave );
  271.         DisableItem( fileMenu, kFMSaveAs );
  272.         DisableItem( fileMenu, kFMPageSetup );
  273.         DisableItem( fileMenu, kFMPrint );
  274.  
  275.         DisableItem( editMenu, kEMPaste );
  276. #ifdef NEVER
  277.         /* STUB Do this differently.  Don't want to redraw menu bar while mouse is down */
  278.         DisableItem( fontMenu, 0 );                /* Disable the entire font menu */
  279.         DisableItem( styleMenu, 0 );            /* Disable the entire style menu */
  280.         DrawMenuBar();
  281. #endif
  282.         return;
  283.     }
  284.  
  285.     SetPort( gDocWindow );
  286.  
  287.     DisableItem( fileMenu, kFMNew );
  288.     DisableItem( fileMenu, kFMOpen );
  289.     EnableItem( fileMenu, kFMClose );
  290.     EnableItem( fileMenu, kFMSave );
  291.     EnableItem( fileMenu, kFMSaveAs );
  292.     EnableItem( fileMenu, kFMPageSetup );
  293.     EnableItem( fileMenu, kFMPrint );
  294.     
  295.     FixPasteItem( editMenu );
  296.  
  297. #ifdef NEVER
  298.     /* STUB Do this differently.  Don't want to redraw menu bar while mouse is down */
  299.     EnableItem( fontMenu, 0 );                /* Enable the entire font menu */
  300.     EnableItem( styleMenu, 0 );                /* Enable the entire style menu */
  301. #endif
  302.     
  303.     textH = (TEHandle)GetWRefCon( gDocWindow );
  304.  
  305.     /* Get the current font */
  306.  
  307. #ifdef NEVER    
  308.     TEGetStyle( (*textH)->selStart, &styleRec, &lineHeight, &fontAscent, textH );
  309. #endif
  310.  
  311.     styleRec.tsFont = 0;
  312.     styleRec.tsSize = 0;
  313.     styleRec.tsFace = 0;
  314.  
  315.     mode = doFont + doFace + doSize;
  316.  
  317.     /* BUG BUG BUG TEContinousStyle does not appear to work as advertised */
  318.     
  319.     continuous = TEContinuousStyle( &mode, &styleRec, textH );
  320.  
  321.     if ( mode & doFont ){
  322.         fontNum = styleRec.tsFont;
  323.         
  324.         if ( fontNum == applFont )
  325.             fontNum = 3 /*GetAppFont()*/;            /* Otherwise Geneva shows up as 1 */
  326.     
  327.         GetFontName( fontNum, fontName );
  328.     }else{
  329.         fontName[ 0 ] = '\0';
  330.     }
  331.  
  332.     numItems = CountMItems( fontMenu );
  333.     
  334.     for ( i = 1; i <= numItems; i++ ){
  335.         GetItem( fontMenu, i, itemName );
  336.         
  337.         if ( PStrCmp( itemName, fontName ) ){
  338.             CheckItem( fontMenu, i, true );
  339.         } else {
  340.             CheckItem( fontMenu, i, false );
  341.         }
  342.     }
  343.     
  344.     /* Show the real font sizes */
  345.     
  346.     for ( i = 0; i < kStMDash2 - kStM9Point; i++ ){
  347.     
  348.         itemSize = gPointSizeTable[ i ];
  349.         
  350.         if ( RealFont( fontNum, itemSize ) ){
  351.             SetItemStyle( styleMenu, i + kStM9Point, outline );
  352.         }else{
  353.             SetItemStyle( styleMenu, i + kStM9Point, 0 );
  354.         }
  355.         
  356.         if ( mode & doSize && itemSize == styleRec.tsSize ){        /* 1.0b2 MDC was fontAscent */
  357.             CheckItem( styleMenu, i + kStM9Point, true );
  358.         } else {
  359.             CheckItem( styleMenu, i + kStM9Point, false );
  360.         }
  361.     }
  362.     
  363.     /* If the styles are the same, check them all, otherwise check none */
  364.     
  365.     for ( i = kStMPlain; i <= kStMExtend; i++ ){
  366.     
  367.         if ( mode & doFace ){
  368.         
  369.             if ( gStyleTable[ i - 1 ] & styleRec.tsFace )
  370.                 CheckItem( styleMenu, i, true );        
  371.         }else{
  372.             CheckItem( styleMenu, i, false );        
  373.         }
  374.     }
  375.      
  376.     return;
  377. }
  378.  
  379. void FixPasteItem( MenuHandle editMenu )
  380. {
  381.     PScrapStuff    scrapPtr;
  382.     long        memFree;
  383.     
  384.     /* It is possible to cause a crash if an application
  385.      * has put a lot on the scrap, even if it is not text.
  386.      * This can happen with Microsoft Word when it puts a
  387.      * huge RTF on the scrap - we crash when we try to paste in
  388.      * the text.  I think this may actually be a bug in System 7.
  389.      * 
  390.      * To prevent this, we disable the paste menu item if
  391.      * there is not enough free memory to hold the whole
  392.      * scrap.  This is not a rigorous test, since FreeMem
  393.      * does not tell how big a single block can be, but it
  394.      * is reasonably fast.
  395.      */
  396.  
  397.      
  398.     memFree = FreeMem();
  399.  
  400.     scrapPtr = InfoScrap();
  401.     
  402.     if ( !scrapPtr || scrapPtr->scrapSize == 0 || scrapPtr->scrapSize > memFree ){
  403.         DisableItem( editMenu, kEMPaste );
  404.         return;
  405.     }
  406.     
  407.     EnableItem( editMenu, kEMPaste );
  408.     
  409.     return;
  410. }